﻿Dim DB_ProjectName
Dim ts
Dim DB_TestcaseExecuted
Dim DB_TestcasesPassed
Dim DB_TestcasesFailed
DB_TestcaseExecuted=0
DB_TestcasesPassed=0
DB_TestcasesFailed=0

'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
'Purpose of this is to create a class and work on the termination
Class FAT
   Private Sub Class_Initialize   ' Setup Initialize event.
		Set DateTime = DotNetFactory.CreateInstance( "System.DateTime" )
		DB_sStartTime= now
		'DB_ProjectName = "YBR"
   End Sub
   Private Sub Class_Terminate   ' Setup Terminate event.

			LogExecutionInfo DB_ProjectName ,DB_TestcaseExecuted,DB_TestcasesPassed,DB_TestcasesFailed,DB_sStartTime

   End Sub
End Class

'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
'Purpose is to create an instance of the FAT class
Set LoggerInstance = New FAT   

'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
'Purpose of this code is to  set the Scenario sheet path and also the scenario_Copy path
sProjectPath = Replace(Environment("TestDir") ,Environment("TestName"),"")
sScenarioFilePath = sProjectPath &"Scenario.xls" 
Copy_sScenarioFilePath=sProjectPath &"Copy_Scenario.xls" 
framework_Reports=sProjectPath &"Reports\"
'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
'Purpose of the code block is to copy the Original scenario file and use it to create a copy of that to make it for Datatable reading
'*****************************************************************************************************************************************************				
				Set objFSO = CreateObject("Scripting.FileSystemObject")
				On error resume next
				On error goto 0
				set srcDoc = getobject (sScenarioFilePath)
				srcDoc.save
				Const OverwriteExisting = True
				objFSO.CopyFile sScenarioFilePath , Copy_sScenarioFilePath, OverwriteExisting
				Set objFSO=nothing
'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
datatable.ImportSheet Copy_sScenarioFilePath,1,1
'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
'Code added for the fetching up the values from the Variable sheet and storing them in Environment variables
datatable.AddSheet("dtVariables")
datatable.ImportSheet Copy_sScenarioFilePath,"Variable_Values","dtVariables"
'*****************************************************************************************************************************************************
'Code to loop the variable sheet for the design time parameters and loop it through the environment string
					For i=1 to DataTable.GetSheet ("dtVariables").GetRowCount
							D_Variable_Name=""
							D_Variable_Value  =""
						   DataTable.SetCurrentRow(i)
						   D_Variable_Name=DataTable.Value("D_Variable_Name","dtVariables")
						   If D_Variable_Name="" Then
							   Exit for
						   End If
						   environment.Value(D_Variable_Name)=DataTable.Value("D_Variable_Value","dtVariables")
					Next
'*****************************************************************************************************************************************************
'Code to loop the variable sheet for the run time parameters and loop it through the environment string
							For i=1 to DataTable.GetSheet ("dtVariables").GetRowCount
							R_Variable_Name=""
							R_Variable_Value  =""
						   DataTable.SetCurrentRow(i)
						   R_Variable_Name=DataTable.Value("R_Variable_Name","dtVariables")
						   If R_Variable_Name="" Then
							   Run_time_row_no=i + 1
							   Exit for
						   End If
						   environment.Value(R_Variable_Name)=DataTable.Value("R_Variable_Value","dtVariables")
					Next

'*****************************************************************************************************************************************************

'*****************************************************************************************************************************************************
'*****************************************************************************************************************************************************
iRow=1
Datatable.SetCurrentRow iRow
sScenarioName = Datatable.GetSheet(1).GetParameter("Scenarios").Value
DB_ProjectName =Datatable.GetSheet(1).GetParameter("Project_Name").Value
sScenarioFlag= Datatable.GetSheet(1).GetParameter("Flag").Value
While sScenarioName<>""

	If  sScenarioFlag<>"N" Then

	time_arr=""
	time_arr=split(time,":")
	Exc_time_val= day(date)&"_"&MonthName(month(date)) &"_"&time_arr(0) &"_"& time_arr(1) &"_" &right(time_arr(2),2)
	Call excel_result(srcDoc,Exc_time_val,iRow +1 ,6,"TestPlan",0)
	sScenarioName_path=framework_Reports & sScenarioName & ".htm"
	OpenFile sScenarioName_path
	intPass=0
	intFail=0
	automated_scenario_count=0

	TestcaseProcessor  sScenarioName

	CloseFile

	time_arr=""
	Exc_time_val=""
	time_arr=split(time,":")
	Exc_time_val= day(date)&"_"&MonthName(month(date)) &"_"&time_arr(0) &"_"& time_arr(1) &"_" &right(time_arr(2),2)
	Call excel_result(srcDoc,Exc_time_val,iRow +1 ,7,"TestPlan",0)

	
	End If
		

	iRow=iRow+1
	Datatable.SetCurrentRow iRow
	sScenarioName = Datatable.Value("Scenarios",dtGlobalSheet)
	sScenarioFlag= Datatable.GetSheet(1).GetParameter("Flag").Value

Wend


Set LoggerInstance= Nothing   




Function  TestcaseProcessor(sSheetName)
 

datatable.ImportSheet Copy_sScenarioFilePath ,sSheetName,2
	'Datatable.ImportSheet sScenarioFilePath,sSheetName,dtlocalSheet
	iTestcaseStepsCnt=Datatable.GetSheet(dtLocalsheet).GetRowCount
	datatable.GetSheet(dtLocalsheet).SetCurrentRow(1)
	DataTable.GetSheet ("dtVariables").SetCurrentRow(1)
	For_Loop_Iteration_Count=1
	 
	
	For i=1 to iTestcaseStepsCnt

		sExecFlag=Datatable.Value("Flag",dtLocalsheet)
		sAction=Datatable.Value("Event",dtLocalsheet)
		environment("FrmParent")=Datatable.Value("Parent",dtLocalsheet)		  
		environment("FrmParent")=Get_Loop_value(environment("FrmParent"))
		environment("FrmChild")=Datatable.Value("Child",dtLocalsheet)							
		environment("FrmChild")=Get_Loop_value(environment("FrmChild"))
		environment("FrmParam1")=Datatable.Value("Param1",dtLocalsheet)
		environment("FrmParam1")=Get_Loop_value(environment("FrmParam1"))
		environment("FrmParam2")=Datatable.Value("Param2",dtLocalsheet)
		environment("FrmParam2")=Get_Loop_value(environment("FrmParam2"))
		environment("FrmParam3")=Datatable.Value("Param3",dtLocalsheet)
		environment("FrmParam3")=Get_Loop_value(environment("FrmParam3"))
		var_link_required=Datatable.Value("Link",dtLocalsheet)
		'**************************************************************************************************************************************************************
		'datatable.GetSheet("Object_Mapping").getrowcount
		
		'**************************************************************************************************************************************************************
		On error resume next 
		environment("FrmObjectMap")=Datatable.Value("ObjectMap",dtLocalsheet)
		On error goto 0
		If  sAction="" Then
				Exit For
		End If
		If Ucase(sAction)<>"TESTCASE"    Then
			     If sExecFlag="Y" Then
							'******************************************************************************************************
							'Changed by vipul mehta on 27th Feb
							Test_case_exceution_counter=Datatable.GetSheet(dtLocalsheet).GetCurrentRow 
							'******************************************************************************************************			
							If trim(ucase(sAction))="STEP" Then
								sAction="Step1"
							End If
							execute (sAction)
							sAction = Trim(sAction)
							automated_steps_count= automated_steps_count + 1
							'******************************************************************************************************
							'Changed by vipul mehta on 27th Feb
							error_code_loop=i
							i=Datatable.GetSheet(dtLocalsheet).GetCurrentRow 
							'******************************************************************************************************
							Select Case error_code
													Case 1			 'When there is a catastropic error and we need to exit the for loop.
																	'Filling the column N to Set the run Flag.
																	 Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																	'Filling the link within the form.
																	 Call excel_result(srcDoc, framework_Reports & sAction  , i+1,16 ,sSheetName,1)
																	'Filling the execution time in the excel sheet.
																	  Call excel_result(srcDoc,Exc_time_val,i+1,19,sSheetName,0)
																	 'Filling Actual result column in the excel sheet
																	  Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)																					
																	  Error_Code=""
																	 'Filling the Execution status in the excel sheet.
																	  Call excel_result(srcDoc,"FAIL",i+1,20,sSheetName,0)
																	  DB_TestcasesFailed=DB_TestcasesFailed + 1
																	  Call AddNewCase(automated_scenario_count&"."&automated_steps_count &" "&sAction,Test_Case_Step, Test_Case_Expected,eval(sAction&"_return"),"FAIL")
																		'*********************************************************************************************************************************
																		'Code added by vipul for the Error handlings
																	catastrop_flag="Y"
																	 Datatable.GetSheet(dtLocalsheet).SetNextRow
																	 sAction=Datatable.Value("Event",dtLocalsheet)
																	 While ucase(trim(sAction))<>"TESTCASE"  and sAction<> ""
																		Datatable.GetSheet(dtLocalsheet).SetNextRow
																		sAction=Datatable.Value("Event",dtLocalsheet)
																	 Wend
																		'*********************************************************************************************************************************		
													Case 0  'When the test case is passed and there is no return value from the function
																	'Filling the column N to Set the run Flag.
																	 Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																	 ' Filling the link within the form.
																	 Call excel_result(srcDoc, framework_Reports & sAction , i+1,16 ,sSheetName,1)
																	 ' Filling the execution time in the excel sheet.
																	  Call excel_result(srcDoc,Exc_time_val,i+1,19,sSheetName,0)
																	  'Filling Actual result column in the excel sheet
																	   Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)
																	     DB_TestcasesPassed=DB_TestcasesPassed+1
																	   Error_Code=""
																		'Filling the Execution status in the excel sheet.
																		Call excel_result(srcDoc,"PASS",i+1,20,sSheetName,0)
																		Call AddNewCase(automated_scenario_count&"."&automated_steps_count &sAction,Test_Case_Step, Test_Case_Expected,eval(sAction&"_return"),"PASS")
														Case 2  'When the functional  error  has occured and  the step is failed
																	    'Filling the column N to Set the run Flag.
																	     Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																		'Filling the link within the form.
																		Call excel_result(srcDoc, framework_Reports & sAction , i+1,16 ,sSheetName,1)
																		'Filling the execution time in the excel sheet.
																		Call excel_result(srcDoc,Exc_time_val,i+1,19,sSheetName,0)
																		'Filling Actual result column in the excel sheet
																		Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)
																	  DB_TestcasesFailed=DB_TestcasesFailed + 1
																		Error_Code=""
																		'Filling the Execution status in the excel sheet.
																		Call excel_result(srcDoc,"FAIL",i+1,20,sSheetName,0)
																		Call AddNewCase(automated_scenario_count&"."&automated_steps_count &sAction,Test_Case_Step, Test_Case_Expected,eval(sAction&"_return"),"FAIL")
														Case 4  'When we have to compare actual result with expected result, function returns something.
																	   'Filling the column N to Set the run Flag.
																	    Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																		'Filling the link within the form.
																		Call excel_result(srcDoc,framework_Reports & sAction , i+1,16 ,sSheetName,1)
																		'Filling the execution time in the excel sheet.
																		Call excel_result(srcDoc,Exc_time_val,i+1,19,sSheetName,0)
																		'Filling Actual result column in the excel sheet
																		Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)
																		Error_Code=""
																		If Ucase(Trim(ExcelSheet.Cells(i+1,17).Value)) =Ucase(Trim(ExcelSheet.Cells(i+1,18).Value)) Then
																			Call excel_result(srcDoc,"PASS",i+1,20,sSheetName,0)
																			html_status="PASS"
																			DB_TestcasesPassed=DB_TestcasesPassed+1
																		else
																			Call excel_result(srcDoc,"FAIL",i+1,20,sSheetName,0)
																			html_status="FAIL"
																	  DB_TestcasesFailed=DB_TestcasesFailed + 1
																		End If																																																																																																
																		Call AddNewCase(automated_scenario_count&"."&automated_steps_count &sAction,Test_Case_Step, Test_Case_Expected,eval(sAction&"_return"),html_status)																																																																																																						
															Case 5 'When we are using the expression to navigate for the branching.
																        Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																		Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)			
																			
																		If ucase(eval(sAction&"_return"))="TRUE" Then
																			Datatable.GetSheet(dtLocalsheet).SetNextRow
																			i=i+1
			
																		elseif ucase(eval(sAction&"_return"))="FALSE"  Then
																				Datatable.GetSheet(dtLocalsheet).SetNextRow
																				i=i+1
																				While "Goto_False"<>Datatable.Value("Event",dtLocalsheet)
																							Datatable.GetSheet(dtLocalsheet).SetNextRow
																							i=i+1
																				wend	
																		else
																			i=i + End_True_return
																		End If
																Case 6
'********************************************************************************************************************************************************
							'Changed by vipul mehta on 27th Feb

																Case 7 ' This is used for  the End Loop purpose.
																	      i=error_code_loop		
																	     Call excel_result(srcDoc, "N" , i+1,1 ,sSheetName,0)
																	    ' Filling the link within the form.
																	     Call excel_result(srcDoc, framework_Reports & sAction , i+1,16 ,sSheetName,1)
																	    ' Filling the execution time in the excel sheet.
																	     Call excel_result(srcDoc,Exc_time_val,i+1,19,sSheetName,0)
																	    'Filling Actual result column in the excel sheet
																	     Call excel_result(srcDoc,eval(sAction&"_return"),i+1,17,sSheetName,0)
																	     DB_TestcasesPassed=DB_TestcasesPassed+1
																	     Error_Code=""
																		'Filling the Execution status in the excel sheet.
																		Call excel_result(srcDoc,"PASS",i+1,20,sSheetName,0)
																		Call AddNewCase(automated_scenario_count&"."&automated_steps_count &sAction,Test_Case_Step, Test_Case_Expected,eval(sAction&"_return"),"PASS")
'********************************************************************************************************************************************************																		
														Case else
																		msgbox "Error code to be defined for "& sAction
										End Select
										DB_TestcaseExecuted=DB_TestcaseExecuted + 1
			       End If
		else

					automated_scenario_count=automated_scenario_count + 1
					automated_steps_count=0

		End if 
		Datatable.GetSheet(dtLocalsheet).SetNextRow
	Next
End Function







